package edu.ucsd.db.bassarisk.algebra.logical;

import java.util.ArrayList;
import java.util.List;

import edu.ucsd.db.bassarisk.algebra.logical.visitor.LogicalOperatorVisitor;
import edu.ucsd.db.bassarisk.query.binding.Bindings.AggregatedValueBinding;

public class AggregateWithin extends LogicalOperator {
  private final List<AggregatedValueBinding> aggs;
  private final LogicalOperator subPlan;

  public AggregateWithin(List<AggregatedValueBinding> aggs, LogicalOperator subPlan) {
    this.aggs = new ArrayList<AggregatedValueBinding>();
    this.aggs.addAll(aggs);
    this.subPlan = subPlan;
  }

  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("AggregateWithin [");
    for (AggregatedValueBinding binding : aggs) {
      sb.append(" ").append(binding.toString()).append(",");
    }
    sb.append("] {").append(subPlan.toString()).append("}");
    return sb.toString();
  }

  @Override
  public <R, A> R accept(LogicalOperatorVisitor<R, A> visitor, A arg) {
    return visitor.visitAggregateWithin(this, arg);
  }

  public LogicalOperator subPlan() {
    return subPlan;
  }

  public Iterable<AggregatedValueBinding> aggregates() {
    return this.aggs;
  }
}
